//线程局部存储由什么用??
//线程局部存储的坑: 
//线程中可以fork吗??线程中可以使用excel程序替换吗??如何理解?
//线程可以fork, 本质是在创建子进程, 可以调用execl, 不过替换的是整个进程, 不建议在线程中excel, 如果要execl
//建议先fork, 再execl 

#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
using namespace std;

__thread pid_t lwp; 
    
__thread string threadname;  //__thread只能存储内置类型，不能存储一些容器


void* threadRoutine(void* args)
{
    string name = static_cast<const char* >(args);
    lwp =  syscall(SYS_gettid); //系统调用获取当前线程的lwp
    while(true)
    {
        cout << "I am new thread" << ", thread name: " << name << "new thread lwp: " << lwp << endl; 
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void*)"thread-1");

    lwp =  syscall(SYS_gettid); //系统调用获取当前线程的lwp
    while(true)
    {
        cout << "I am new thread" << "new thread lwp: " << lwp << endl; 
        sleep(1);
    }

    pthread_join(tid, nullptr); 
    return 0;
}